package com.chaoyong.higo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.ProtailAdapter;
import com.chaoyong.higo.adapter.WinningNoAdapter;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.UserListBean;
import com.chaoyong.higo.bean.UserListBean.DataEntity.ListEntity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.Values;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 2016.3.2
 * 
 * @author 丁晶
 *
 * 最新揭晓详情
 */
public class ProductDetailActivity extends BaseActivity {
	@Override
	public int layoutResID() {
		return R.layout.activity_product_detail;
	}

	private Context context;
	private TextView title_center_text;
	private TextView title_right_text;
	// 参加号码
	private RecyclerView recyclerView;
	// 参与用户
	private RecyclerView recyclerVie;

	private ImageView detail_shar;
	
	private String goods_id,expect;
	
	private String goods_img,head_img;
	private ImageView iv_image,iv_photo;
	private TextView username,diqu,begin_time,end_time,win_no,renshu;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private LinearLayout ll_no;
	private List<Integer> code;
	
	private LinearLayout ll_more;
	private List<ListEntity> entity;
	private ProtailAdapter adapter;
	
	private Button jisuan;
	private int length;
	
	private String price,time;
	
	private RelativeLayout tuwenxiangqing;
	private String goodsInfo;
	
	@Override
	public void findView() {
		title_center_text = (TextView) findViewById(R.id.title_center_text);
		title_right_text = (TextView) findViewById(R.id.title_right_text);
		recyclerView = (RecyclerView) findViewById(R.id.prodetail_number_recycler);
		recyclerVie = (RecyclerView) findViewById(R.id.prodetail_user_recycler);
		//detail_shar = (ImageView) findViewById(R.id.detail_shar);

		//detail_shar.setOnClickListener(this);
		
		iv_image = (ImageView) findViewById(R.id.winning_goods_pic);
		iv_photo = (ImageView) findViewById(R.id.winning_goods_photo);
		
		username = (TextView) findViewById(R.id.winning_goods_username);
		diqu = (TextView) findViewById(R.id.winning_goods_diqu);
		begin_time = (TextView) findViewById(R.id.winning_goods_begin_time);
		end_time = (TextView) findViewById(R.id.winning_goods_end_time);
		win_no = (TextView) findViewById(R.id.winning_goods_win_no);
		renshu = (TextView) findViewById(R.id.winning_goods_renshu);
		
		ll_no = (LinearLayout) findViewById(R.id.winning_goods_ll_no);
		ll_more = (LinearLayout) findViewById(R.id.winning_goods_ll_no_more);
		
		tuwenxiangqing = (RelativeLayout) findViewById(R.id.tuwenxiangqing);
		
		jisuan = (Button) findViewById(R.id.winning_goods_jisuan);
		jisuan.setOnClickListener(this);
		
		ll_more.setOnClickListener(this);
		tuwenxiangqing.setOnClickListener(this);
	}

	@Override
	public void init() {
		context = this;
		
		
		
		title_center_text.setText("商品详情");
		
		title_right_text.setOnClickListener(this);
		
		
		
		// 参加号码
		recyclerView.setAdapter(new MyAdapters());
		recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
		// 参加用户
		recyclerVie.setLayoutManager(new LinearLayoutManager(context));
		adapter = new ProtailAdapter(context, null);
		 recyclerVie.setAdapter(adapter);
		
		goods_img = getIntent().getStringExtra("goods_img");
		head_img = getIntent().getStringExtra("head_img");
		
		goodsInfo = getIntent().getStringExtra("goodsInfo");
		
		ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + goods_img, iv_image);
		ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + head_img, iv_photo);
	}
	

	class MyAdapters extends RecyclerView.Adapter<MyAdapters.MyViewHolder> {
		class MyViewHolder extends ViewHolder {

			public MyViewHolder(View arg0) {
				super(arg0);
			}

		}

		@Override
		public int getItemCount() {
			return 8;
		}

		@Override
		public void onBindViewHolder(MyViewHolder arg0, int arg1) {

		}

		@Override
		public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
			MyViewHolder holder = new MyViewHolder(
					LayoutInflater.from(context).inflate(R.layout.item_prodetail_number, arg0, false));
			return holder;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_right_text:
			startActivity(new Intent(context, SeleCounActivity.class));
			break;
		case R.id.detail_shar:
			startActivity(new Intent(context, SharActivity.class));
			break;
		case R.id.winning_goods_ll_no_more:
			Intent intent = new Intent(context, WinningNoActivity.class);
			intent.putIntegerArrayListExtra("datas", (ArrayList<Integer>) code);
			startActivity(intent);
			break;
		case R.id.winning_goods_jisuan:
			Intent intentCount = new Intent();
			intentCount.setClass(context, CountActivity.class);
			intentCount.putExtra("goods_id", goods_id);
			intentCount.putExtra("expect", expect);
			intentCount.putExtra("length", Integer.parseInt(price.split("\\.")[0]));
			intentCount.putExtra("time", time);
			startActivity(intentCount);
			break;
		case R.id.tuwenxiangqing:
			Intent intentt = new Intent(context, ImagDetailActivity.class);
			intentt.putExtra("info", goodsInfo);
			startActivity(intentt);
			break;
		default:
			break;
		}
	}

	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		goods_id = getIntent().getStringExtra("goods_id");
		expect = getIntent().getStringExtra("expect");
		price = getIntent().getStringExtra("price");
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("goods_id", goods_id);
			info.put("expect", expect);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			json.put("path", "/M/Goods/winningDatail");
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_winningGoods, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject js = new JSONObject(result);
					JSONObject data = js.getJSONObject("data");
					JSONObject jiben = data.getJSONObject("expect");
					JSONObject buy_time = data.getJSONObject("buy_time");
					
					JSONArray user_code = data.getJSONArray("user_code");
					length = user_code.length();
					code = new ArrayList<Integer>();
					for (int i = 0; i < length; i++) {
						String uc = (String) user_code.get(i);
						code.add(Integer.parseInt(uc) + 10000000);
					}
					
					//if(user_code.length() < 5){
						/*for (int i = 0; i < 4; i++) {
							TextView tv = new TextView(context);
							tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
							tv.setGravity(Gravity.CENTER);
							tv.setText(code.get(i) + "");
							ll_no.addView(tv);
						}*/
					//}
						
						switch (length) {
						case 1:
							TextView tv = new TextView(context);
							tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
							tv.setGravity(Gravity.CENTER);
							tv.setText(code.get(0) + "");
							ll_no.addView(tv);
							break;
						case 2:
							for (int i = 0; i < 2; i++) {
								TextView tv2 = new TextView(context);
								tv2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
								tv2.setGravity(Gravity.CENTER);
								tv2.setText(code.get(i) + "");
								ll_no.addView(tv2);
							}
							break;
						case 3:
							for (int i = 0; i < 3; i++) {
								TextView tv3 = new TextView(context);
								tv3.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
								tv3.setGravity(Gravity.CENTER);
								tv3.setText(code.get(i) + "");
								ll_no.addView(tv3);
							}
							break;
						default:
							for (int i = 0; i < 4; i++) {
								TextView tv4 = new TextView(context);
								tv4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
								tv4.setGravity(Gravity.CENTER);
								tv4.setText(code.get(i) + "");
								ll_no.addView(tv4);
							}
							break;
						}
						title_right_text.setText("第" + jiben.getString("expect") + "期");
					//username.setText("恭喜" + Html.fromHtml("<font color='#ff0000'>的神烦大叔</font>") + jiben.getString("user_name") + "获得该期商品");
						
					username.setText(Html.fromHtml("恭喜<font color='#ff0000'>" + jiben.getString("user_name") + "</font>获得该期商品"));
					diqu.setText("北京市朝阳区");
					time = sdf.format(new Date(jiben.getLong("lottery_time") * 1000L));
					
					begin_time.setText(sdf.format(new Date(Long.parseLong(buy_time.getString("time").split("\\.")[0]) * 1000L)));
					end_time.setText(sdf.format(new Date(jiben.getLong("lottery_time") * 1000L)));
					win_no.setText(jiben.getInt("winning") + 10000000 + "");
					renshu.setText(length + "");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		userlist(goods_id, expect);
	}
	
	private void userlist(String goodsId, String goodsExpe) {
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		JSONObject object = new JSONObject();
		try {
			object.put("model", "GoodsRecord");
			object.put("function", "getRecord");
			object.put("app", "1");
			object.put("code", goodsId + "," + goodsExpe);
			params.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		// param.setBodyEntity(new StringEntity(jsonObject.toString(),
		// "UTF-8"));
		utils.send(HttpMethod.POST, Values.MAIN_RECOMMEND, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {

			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				if (result != null) {
					JSONObject object;
					try {
						object = new JSONObject(result);
						if (object.getString("status").equals("1")) {
							Gson gson = new GsonBuilder().create();
							UserListBean bean = gson.fromJson(result, UserListBean.class);
							entity = bean.getData().getList();
							adapter.changeData(entity);
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

		});
	}
	
	public void onResult(View view){
		JSONObject json = new JSONObject();
		try {
			json.put("goods_id", goods_id);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_getGoodsDatail, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		ll_no.removeAllViews();
	}
}
